﻿

using System.Net.Sockets;

namespace Company.Plc
{
    public abstract class PlcBase : IPlc
    {
        protected TcpClient tcpClient;
        protected PlcConfig plcConfig;
        public bool Initialized { get; private set; }

        public bool Connected { get; private set; }

        public string Message { get; private set; }

        bool IPlc.Initialized => Initialized;

        public virtual void Close()//虚方法在子类中可以选择性重写
        {
            Connected = false;
            Message = "Connection closed.";
        }

        public  bool Init(PlcConfig config)
        {
            if(Initialized) return Initialized;
            plcConfig = config;
            bool result = DoInit();
           if(result)
            {
                Initialized = true;
                Connected = true;
                Message = "Initialization successful.";
            }
            else
            {
                Initialized = false;
                Connected = false;
                Message = "Initialization failed.";
            }
            return result;
        }
        /// <summary>
        /// 在子类中实现具体的初始化逻辑
        /// </summary>
        /// <returns></returns>
        /// 
        public abstract bool DoInit();

        public abstract ushort[] Read(string address, ushort length);

        public abstract bool Write(string address, ushort[] values);

        public abstract bool Write(string address, ushort values);
        
    }
}
